var config = require('../../../utils/config.js');
var util = require('../../../utils/util.js');
var API_URL = config.API_URL;
var app = getApp();
var id, page, loading, hasMore;
var num;
var arr;
var getStar = function (num) {
    console.log(num)
    if (num == "") {
        num = 5;
    }
    arr = [];
    for (var i = 0; i < 5; i++) {
        if (i < num) {
            arr.push('star');
        } else {
            arr.push('star-w');
        }
    }
    return arr

}
var getData = that => {
    if (loading) {
        return
    }
    loading = true;
    let params = {
        goods_id: id,
        page: page
    }
    util.POST(API_URL.comments, params)
        .then(res => {
            console.log(res)
            var comments;
            if (page == 1) {
                comments = new Array();
            } else {
                comments = that.data.comments;
            }
            let list = res.data.data.list.filter((item) => {
                return item != null;
            });
            console.log(list)
            comments = comments.concat(list);
            if (res.data.data.list.length != 0) {
                page++
            }
            that.setData({
                comments: comments,
            })
            hasMore = res.data.data.hasMore
            loading = false;
            my.hideLoading();
            my.stopPullDownRefresh();
        }).catch(res => {
            loading = false;
            my.hideLoading();
            my.stopPullDownRefresh();
        })
}
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //   拿到该商品的id 
        id = options.id;
        page = 1;
        loading = false;
        getData(this)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        setTimeout(function () {
            num = that.data.comments[0].starLevel;
            getStar(num)
            that.setData({
                arr: arr
            })
        }, 1000)

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        page = 1;
        loading = false;
        getData(this)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (hasMore == 1) {
            my.showLoading({
                content: "正在加载",
                success: (res) => {
                    getData(this);
                }
            });
            setTimeout(() => {
                my.hideLoading();
            }, 1000)
        } else {
            my.showToast({
                content: '没有更多的内容了',
                duration: 2000
            });
        }

        console.log(hasMore)

    },
    like: function (e) {
        let that = this;
        //   获取该评论的id
        console.log(e)
        let id = e.currentTarget.dataset.id;
        util.PRL(API_URL.goods_comment_like, { comment_id: id })
            .then(res => {
                console.log(res)
                if (res.data.status == 300) {
                    my.alert({
                        title: '提示',
                        content: res.data.msg,
                        buttonText: '确定'
                    });
                } else {
                    let comments = that.data.comments;
                    let like = Number(comments.like);
                    comments.like = like + 1;
                    that.setData({
                        comments: comments
                    })
                    my.showToast({
                        content: res.data.msg,
                        type: 'success',
                        duration: 600,
                        success: () => {
                            setTimeout(function () {
                                my.navigateTo({
                                    url: '/pages/good/comment_detail/comment_detail?id=' + id,
                                })
                            }, 1000);
                        }
                    })
                }
            }).catch(res => {
                console.log(res);
            })
    },
    goToInfo: function (e) {
        let id = e.currentTarget.dataset.id;
        my.navigateTo({
            url: '/pages/good/comment_detail/comment_detail?id=' + id,
        })
    }
})